/*
 * @# SmsTestServlet.java 2011-11-21 下午04:04:09
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.experiments.ws.sms.test;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import cn.eshore.bnet.sag.sms.DeliveryState;
import cn.eshore.bnet.sag.sms.PolicyException_Exception;
import cn.eshore.bnet.sag.sms.ServiceException_Exception;

import com.ianswer.common.sms.SmsDispatcher;
import com.ianswer.common.sms.ct.CtSmsQuery;

/**
 * 短信发送测试servlet
 * 
 * @author tian qingsen
 */
public class SmsTestServlet extends HttpServlet {

	private static final long serialVersionUID = -8535754130706286794L;
	private static Logger log = Logger.getLogger(SmsTestServlet.class);

	@Override
	public void init() throws ServletException {
		super.init();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String testContent = req.getParameter("testContent");
		if ("getState".equalsIgnoreCase(testContent)) {
			getSmsState(req, resp);
		} else {
			sendSms(req, resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}
	
	private void sendSms(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String receiver = req.getParameter("receiver");
		String content = req.getParameter("content");
		String notifyUrl = req.getParameter("notifyUrl");
		
		if (StringUtils.isNotBlank(receiver) && StringUtils.isNotBlank(content)) {
			SmsDispatcher dispatcher = new SmsDispatcher();
			if (StringUtils.isNotBlank(notifyUrl)) {
				dispatcher.sendAndNotify(receiver, content, notifyUrl);
			} else {
				dispatcher.send(receiver, content);
			}
		}
		
		req.setAttribute("receiver", receiver);
		req.setAttribute("content", content);
		req.getRequestDispatcher("/smsTest.jsp").forward(req, resp);
	}
	
	private void getSmsState(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String requestId = req.getParameter("requestId");
		List<DeliveryState> stateList = new ArrayList<DeliveryState>();
		if (StringUtils.isNotBlank(requestId)) {
			CtSmsQuery csq = new CtSmsQuery();
			try {
				stateList = csq.getState(requestId);
			} catch (ServiceException_Exception e) {
				log.error("getSmsState fail, error detail: [" + e.getErrorCode() + "] " + e.getErrorMessage(), e);
			} catch (PolicyException_Exception e) {
				log.error("getSmsState fail, error detail: [" + e.getErrorCode() + "] " + e.getErrorMessage(), e);
			} catch (ParseException e) {
				log.error("getSmsState fail, error detail: [" + e.getMessage() + "]", e);
			}
		}
		
		String stateStr = "";
		for (DeliveryState state : stateList) {
			stateStr = ("[receive number: " + state.getReceiver() + ", state: " + state.getState() + "]");
		}
		req.setAttribute("requestId", requestId);
		req.setAttribute("stateStr", stateStr);
		req.getRequestDispatcher("/smsStateQuery.jsp").forward(req, resp);
	}

	@Override
	public void destroy() {
		super.destroy();
	}

}
